package com.moorror.codegenvel.common.tree;

import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.project.Project;
import com.intellij.util.concurrency.NonUrgentExecutor;
import com.moorror.codegenvel.view.component.CodeGenByVelocityComponent;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;

/**
 * @Author jiyanghuang
 * @Date 2022/7/17 20:50
 */
public interface TreePanelMark {

    public default DefaultMutableTreeNode rootTreeNode() {
        return null;
    }

    public default JTree jTree() {
        return null;
    }

    /**
     * 是否展示右边内容
     * 默认展示
     *
     * @return
     */
    public default boolean showRightContent() {
        return true;
    }

    public default Project fetchProject(){
        return null;
    }

    public String tabName();

    public default void selectTab() {
        CodeGenByVelocityComponent.getInstance(fetchProject()).selectTab(tabName());
    }

    public default void treeReload(TreeNode nodeRefresh, Runnable runnable) {
        /**
         * 查询新并更新
         */
        ReadAction.nonBlocking(() -> {
            if (null != runnable) {
                runnable.run();
            }
            return null;
        }).finishOnUiThread(ModalityState.NON_MODAL, (c) -> {
            ((DefaultTreeModel) jTree().getModel()).reload(nodeRefresh);
        }).inSmartMode(fetchProject()).submit(NonUrgentExecutor.getInstance());
    }
}
